/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LoanDetailUI.java
 *
 * Created on 2010.11.09., 13:24:59
 */
package workshopdesign.UI;

import java.text.SimpleDateFormat;
import javax.swing.JOptionPane;
import javax.swing.border.TitledBorder;
import workshopdesign.Control.LoanController;
import workshopdesign.Model.Loan;

/**
 *
 * @author Dom
 */
public class LoanDetailUI extends javax.swing.JPanel {

    private Loan loan = null;
    private LoanController loan_controller = LoanController.getLoanController();
    private SimpleDateFormat sdf = (SimpleDateFormat) SimpleDateFormat.getInstance();

    private BaseUIController parent;

    /** Creates new form LoanDetailUI */
    public LoanDetailUI(BaseUIController parent, String title, boolean enable_actions) {
        initComponents();

        this.parent = parent;

        this.setBorder(new TitledBorder(title));

        ok_button.setVisible(enable_actions);
        cancel_button.setVisible(enable_actions);

        clear_form();
    }

    public Loan getLoan() {
        return loan;
    }

    public void setLoan(Loan loan) {
        this.loan = loan;
        load_data();
    }

    public void load_data() {
        update_buttons();
        
        if (loan == null) {
            clear_form();
            return;
        }
        borrow_date_field.setText(sdf.format(loan.getBorrowDate()));
        period_field.setText(String.valueOf(loan.getPeriod()));
        state_field.setText(loan.getState().toString());
        expires_field.setText(sdf.format(loan.getExprationDate()));
    }

    private void clear_form() {
        borrow_date_field.setText("");
        period_field.setText("");
        state_field.setText("");
        expires_field.setText("");
    }

    private void update_buttons() {
        boolean enabled = (loan != null) && loan.getState() != Loan.State.returned;

        ok_button.setEnabled(enabled);
        cancel_button.setEnabled(enabled);
    }

    public void clear_loan() {
        loan = null;
        clear_form();
        update_buttons();
    }

    public void extend_load() {
        if (loan.getState() == Loan.State.returned) {
            return;
        }

        int period = 0;

        try{
            period = Integer.parseInt(JOptionPane.showInputDialog("With how many days you want to extend the loan?", 5));
        } catch (Exception e){
            return;
        }

        loan_controller.extend_loan(loan.getId(), period);

        parent.update_data();
        load_data();
    }

    public void return_dvd() {
        loan_controller.returned_copy(loan.getId());
        parent.update_data();
        load_data();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        expires_field = new javax.swing.JLabel();
        state_field = new javax.swing.JTextField();
        period_field = new javax.swing.JTextField();
        borrow_date_field = new javax.swing.JTextField();
        ok_button = new javax.swing.JButton();
        cancel_button = new javax.swing.JButton();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(workshopdesign.WorkshopDesignApp.class).getContext().getResourceMap(LoanDetailUI.class);
        setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("Form.border.title"))); // NOI18N
        setName("Form"); // NOI18N

        jLabel10.setText(resourceMap.getString("jLabel10.text")); // NOI18N
        jLabel10.setName("jLabel10"); // NOI18N

        jLabel11.setText(resourceMap.getString("jLabel11.text")); // NOI18N
        jLabel11.setName("jLabel11"); // NOI18N

        jLabel12.setText(resourceMap.getString("jLabel12.text")); // NOI18N
        jLabel12.setName("jLabel12"); // NOI18N

        jLabel14.setText(resourceMap.getString("jLabel14.text")); // NOI18N
        jLabel14.setName("jLabel14"); // NOI18N

        expires_field.setText(resourceMap.getString("expires_field.text")); // NOI18N
        expires_field.setName("expires_field"); // NOI18N

        state_field.setEditable(false);
        state_field.setText(resourceMap.getString("state_field.text")); // NOI18N
        state_field.setName("state_field"); // NOI18N

        period_field.setEditable(false);
        period_field.setText(resourceMap.getString("period_field.text")); // NOI18N
        period_field.setName("period_field"); // NOI18N

        borrow_date_field.setEditable(false);
        borrow_date_field.setText(resourceMap.getString("borrow_date_field.text")); // NOI18N
        borrow_date_field.setName("borrow_date_field"); // NOI18N

        ok_button.setText(resourceMap.getString("ok_button.text")); // NOI18N
        ok_button.setName("ok_button"); // NOI18N
        ok_button.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                ok_buttonMouseClicked(evt);
            }
        });

        cancel_button.setText(resourceMap.getString("cancel_button.text")); // NOI18N
        cancel_button.setName("cancel_button"); // NOI18N
        cancel_button.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cancel_buttonMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 207, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel10)
                    .addComponent(jLabel11)
                    .addComponent(jLabel12)
                    .addComponent(jLabel14))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(period_field, javax.swing.GroupLayout.DEFAULT_SIZE, 142, Short.MAX_VALUE)
                    .addComponent(borrow_date_field, javax.swing.GroupLayout.DEFAULT_SIZE, 142, Short.MAX_VALUE)
                    .addComponent(expires_field, javax.swing.GroupLayout.DEFAULT_SIZE, 142, Short.MAX_VALUE)
                    .addComponent(state_field, javax.swing.GroupLayout.DEFAULT_SIZE, 142, Short.MAX_VALUE)))
            .addGroup(layout.createSequentialGroup()
                .addComponent(ok_button)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 75, Short.MAX_VALUE)
                .addComponent(cancel_button))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 132, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(borrow_date_field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(period_field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(state_field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(expires_field)
                    .addComponent(jLabel14))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ok_button)
                    .addComponent(cancel_button)))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void ok_buttonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ok_buttonMouseClicked
        extend_load();
    }//GEN-LAST:event_ok_buttonMouseClicked

    private void cancel_buttonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cancel_buttonMouseClicked
        return_dvd();
    }//GEN-LAST:event_cancel_buttonMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField borrow_date_field;
    private javax.swing.JButton cancel_button;
    private javax.swing.JLabel expires_field;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JButton ok_button;
    private javax.swing.JTextField period_field;
    private javax.swing.JTextField state_field;
    // End of variables declaration//GEN-END:variables
}
